﻿Imports System.Xml

Public Class XmlHelper
    Public Shared Function ParseXmlEnum(Of t As Structure)(value As String) As t?
        Dim val As t
        If [Enum].TryParse(Of t)(value, True, val) Then
            Return val
        Else
            Return Nothing
        End If
    End Function

    Public Shared Function GetAttributeOrDefault(Of T)(reader As XmlReader, attributeName As String, defaultValue As T) As T
        Dim val As Object = reader.GetAttribute(attributeName)
        If val IsNot Nothing Then
            Return CType(val, T)
        Else
            Return defaultValue
        End If
    End Function

    Public Shared Sub WriteAttributeIfNotDefault(writer As XmlWriter, attributeName As String, value As String, [default] As String)
        If value <> [default] Then
            writer.WriteAttributeString(attributeName, value)
        End If
    End Sub


    Public Shared Sub WriteAttributeIfNotEmpty(writer As XmlWriter, attributeName As String, value As String)
        If value <> "" Then
            writer.WriteAttributeString(attributeName, value)
        End If
    End Sub


End Class
